<template>
	<div class="wrapper">

		<!-- header部分 -->
		<header>
			<p>地址管理</p>
		</header>

		<!-- 地址列表部分 -->
		<ul class="addresslist">
			<li v-for="(item,index) in deliveryAddressArr">
				<div class="addresslist-left" @click="setAddress(item,index)">
					<h3>{{item.contactName}} {{item.contactSex==0?"女":"男"}} {{item.contactTel}}</h3>
					<p>{{item.address}}</p>
				</div>
				<div class="addresslist-right">
					<i class="fa fa-edit" @click="toEdit(item.daId)"></i>
					<i class="fa fa-remove" @click="deleteAddress(item.daId)"></i>
				</div>
			</li>
		</ul>

		<!-- 新增地址部分 -->
		<div class="addbtn" @click="toAddUserAddress()">
			<i class="fa fa-plus-circle"></i>
			<p>新增收货地址</p>
		</div>

	</div>
</template>

<script>
export default {
data(){
return{
	businessId:this.$route.query.businessId,
	deliveryAddressArr:[],
	user:{}
}
},
created(){
	 this.user = this.$getSessionStorage('user');
	     this.selectAddress();
		 console.log(this.businessId)
},
methods:{
	// 根据userId查询该用户对应的地址，并且在删除地址后，调用该方法，重新查询
	selectAddress(){ 
		this.$axios.post('DeliveryAddressController/listDeliveryAddressByUserId',this.$qs.stringify({
		userId:this.user.userId
	 })).then(response=>{
		this.deliveryAddressArr=response.data
	 }).catch(error=>{
            console.log(error)
        })},
		// 用户点击某一地址，会将改地址存到本地存储中，然后返回到order页面显示选择的地址
	setAddress(Address,index){
		console.log(Address);
		console.log(index)
		for(let i=0;i<this.deliveryAddressArr.length;i++){
			if(i==index){
				//把该用户选择的地址存到localStorage中
		this.$setLocalStorage('address',Address);
		this.$router.push({
			path:'/orders',
			query: {
						businessId:this.businessId
					}
			});
			}
		}
		
	},
	toAddUserAddress(){
		this.$router.push({
					path:'/adduseraddress',
					query: {
						businessId:this.businessId
					}
				});
	},
	toEdit(daId){
		this.$router.push({
					path:'/edituseraddress',
					query:{
					daId:daId,
					businessId:this.businessId
				}
				});
	},
	deleteAddress(daId){
		this.$axios.post('DeliveryAddressController/removeDeliveryAddress',this.$qs.stringify({
			daId: daId
		})).then(response=>{
			// 如果请求成功，要在本地存储中查找该地址，并且判断改地址是否为选择要删除的地址，
			// 然后调用removeLocalStorage根据key值删除该地址，最后调用selectAddress方法刷新该页面地址
			if(response.data==1){
         let deliveryAddress = this.$getLocalStorage('address');
		if(deliveryAddress!=null&&deliveryAddress.daId==daId){
			this.$removeLocalStorage('address');
		}
		this.selectAddress();
		}else{
				alert("删除失败！！！")
			}
        }).catch(error=>{
            console.log(error)
        })
	}
}

}
 
</script>

<style scoped>
	/*************** 总容器 ***************/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: #F5F5F5;
	}

	/*************** header ***************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		display: flex;
		justify-content: space-around;
		align-items: center;
		color: #fff;
		font-size: 4.8vw;
		position: fixed;
		left: 0;
		top: 0;
		/*保证在最上层*/
		z-index: 1000;
	}

	/*************** addresslist ***************/
	.wrapper .addresslist {
		width: 100%;
		margin-top: 12vw;
		background-color: #fff;
	}

	.wrapper .addresslist li {
		width: 100%;
		box-sizing: border-box;
		border-bottom: solid 1px #DDD;
		padding: 3vw;

		display: flex;
	}

	.wrapper .addresslist li .addresslist-left {
		flex: 5;
		/*左边这块区域是可以点击的*/
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addresslist li .addresslist-left h3 {
		font-size: 4.6vw;
		font-weight: 300;
		color: #666;
	}

	.wrapper .addresslist li .addresslist-left p {
		font-size: 4vw;
		color: #666;
	}

	.wrapper .addresslist li .addresslist-right {
		flex: 1;
		font-size: 5.6vw;
		color: #999;
		cursor: pointer;

		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	/*************** 新增地址部分 ***************/
	.wrapper .addbtn {
		width: 100%;
		height: 14vw;
		border-top: solid 1px #DDD;
		border-bottom: solid 1px #DDD;
		background-color: #fff;
		margin-top: 4vw;

		display: flex;
		justify-content: center;
		align-items: center;

		font-size: 4.5vw;
		color: #0097FF;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addbtn p {
		margin-left: 2vw;
	}
</style>
